<template>
  <div class="login">
    <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" class="container">
        <el-form-item>
        <div class="avatar">
          <img src="../../assets/logo.jpg" alt="">
        </div>
      </el-form-item>
      <el-form-item label="用户名" prop="username">
        <el-input type="text" v-model="ruleForm.username" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input type="password" v-model="ruleForm.password" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { checkLogin } from "../../api/index";

export default {
  data() {
    //设置正则验证规则
    var validateUserName = (rule, value, callback) => {
      if (!/^[a-zA-Z]\w+$/.test(value))
        return callback(new Error("用户名不能以数字或者特殊符号开头"));
      callback();
    };

    return {
      ruleForm: {
        username: "admin",
        password: "123456",
      },
      rules: {
        //设置用户名与密码的验证
        username: [
          { required: true, message: "请输入登录名称", trigger: "blur" },
          { min: 3, max: 12, message: "长度在3-12个字符", trigger: "blur" },
          { validator: validateUserName, trigger: "blur" },
        ],
        password: [
          { required: true, message: "请输入登录名称", trigger: "blur" },
          { min: 3, max: 12, message: "长度在3-12个字符", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    //登录发送请求
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          //获取用户输入的数据
          //发送请求
          checkLogin(this.ruleForm).then((res) => {
            console.log(res);
            // 拿到后台返回的数据 进行判断 是否登录成功
            let {
              meta: { status, msg },
              data,
            } = res;
            console.log(msg);
            //不成功则返回错误信息
            if (status != 200) return this.$message.error(msg);
            //成功则将用户信息储存到本地储存并跳转到home页面
            this.$message.success(msg);
            let { username, token } = data;
            let newObj = { username, token };
            localStorage.setItem("userName", JSON.stringify(newObj));
            //登录成功使用vuex储存用户名
            this.$store.commit('setUsername', res.data.username)
            this.$router.push("/");
          });
        }
      });
    },
  },
};
</script>

<style lang="less" scoped>
.login {
  position: fixed;
  width: 100%;
  height: 100%;
  background-color: #2f4050;

  .container {
    position: absolute;
    left: 0;
    right: 0;
    width: 400px;
    padding: 0px 40px 15px 40px;
    margin: 200px auto;
    background: white;
    .avatar {
      position: relative;
      left: 50%;
      width: 120px;
      height: 120px;
      margin-left: -60px;
      margin-top: -60px;
      box-sizing: border-box;
      border-radius: 50%;
      border: 10px solid #fff;
      box-shadow: 0 1px 5px #ccc;
      overflow: hidden;
      img{
          width: 100%;
          height: 100%;
      }
    }
    .login-btn {
      width: 100%;
    }
  }
}
</style>